package com.gadgets.cm04.fileregix.service;

import com.gadgets.cm04.fileregix.dto.FileRegixQueryDTO;
import com.gadgets.cm04.fileregix.pojo.FileRegixInfo;
import com.gadgets.cm04.fileregix.repository.IFileRegixInfoRepository;
import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.exception.GadgetsException;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.gadgets.framework.common.util.PaginationUtil;
import com.gadgets.framework.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2020-09-16
 * 
 */
@Service
@Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class)
public class FileRegixLogicImpl implements FileRegixLogic {

    private static final Logger logger = LoggerFactory.getLogger(FileRegixLogicImpl.class);

    @Autowired
    private IFileRegixInfoRepository fileRegixInfoRepository;

    /**
     * 查询文件上传/下载规则信息（分页查询）
     *
     * @param queryDTO   查询条件
     * @param pagination 分页条件
     *
     * @return 查询结果
     *
     * @throws GadgetsServiceException 异常信息
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class, propagation = Propagation.SUPPORTS)
    @Override
    public List<FileRegixInfo> queryFileRegixInfo(FileRegixQueryDTO queryDTO, Pagination pagination) throws GadgetsException {
        try {
            PaginationUtil.bulidPaginationInfo(pagination);
            List<FileRegixInfo> regixInfos = fileRegixInfoRepository.queryFileRegixInfo(queryDTO);
            PaginationUtil.getPaginationResultInfo(regixInfos, pagination);
            return regixInfos;
        } catch (Exception exception) {
            exception.printStackTrace();
            logger.error(StringUtils.formatExceptionStack(exception));
            throw new GadgetsServiceException(exception);
        }
    }

}
